//
//  SimpleRegistController.m
//  HappyBaby
//
//  Created by 华哥 on 15/11/28.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "SimpleRegistController.h"
#import "Define.h"
#import <BmobSDK/Bmob.h>
#import "SimpleRegistView.h"
#import "FindPasswordController.h"
@interface SimpleRegistController ()
@property (nonatomic, strong) SimpleRegistView *simpleRegistView;
@end

@implementation SimpleRegistController

- (void)loadView
{
    self.simpleRegistView = [[SimpleRegistView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.view = _simpleRegistView;
    
    // 获取验证码
    [self.simpleRegistView.securityCodeBtn addTarget:self action:@selector(securityCodeBtn) forControlEvents:UIControlEventTouchUpInside];
    // 点击快速注册button
    [self.simpleRegistView.registButton addTarget:self action:@selector(simpleRegist) forControlEvents:UIControlEventTouchUpInside];
    
    // 如果点击是从忘记密码button过来的那么执行下面的过程
    if ([self.forgetPasswordFlag isEqualToString:@"忘记密码"]) {
        self.navigationItem.title = @"修改密码";
        [self.simpleRegistView.registButton setTitle:@"下一步" forState:UIControlStateNormal];
        [self.simpleRegistView.registButton addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
        self.simpleRegistView.userProtocol.text = @"点击下一步重置密码";
        self.simpleRegistView.userProtocolBtn.hidden = YES;
        
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"快速注册";
}

- (void)securityCodeBtn
{
    NSString *phoneNumber = self.simpleRegistView.userPhone.text;
    //    发送手机验证码;
    [BmobSMS requestSMSCodeInBackgroundWithPhoneNumber:phoneNumber andTemplate:@"userRegist" resultBlock:^(int number, NSError *error) {
        if (error)
        {
            NSLog(@"发送失败");
        } else {
            NSLog(@"发送成功");
        }
    }];
}
#pragma mark --点击注册按钮注册方法
- (void)simpleRegist
{
    // 验证用户的手机号
    NSString *phoneNumber = self.simpleRegistView.userPhone.text;
    // 输入的验证码,
    NSString *verifyText = self.simpleRegistView.securityCode.text;
    //    先验证手机验证码, 判断输入验证是否正确
    [BmobSMS verifySMSCodeInBackgroundWithPhoneNumber:phoneNumber andSMSCode:verifyText resultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful)
        {
            // 如果验证成功执行注册方法
            [self verifySuccess];
        }
        else
        {
            // 注册失败,执行方法
            [self verityFail];
        }
    }];
}
#pragma mark --找回密码
- (void)next
{
    // 跳转到找回密码界面
    FindPasswordController * findPassWord = [[FindPasswordController alloc] init];
    [self.navigationController pushViewController:findPassWord animated:YES];
}



#pragma mark --------------- 手机验证码执行方法 ----------------------
// 验证成功注册
- (void)verifySuccess
{
    // 验证用户的手机号
    NSString *phoneNumber = self.simpleRegistView.userPhone.text;
    // 输入的验证码,
    NSString *userPassword = self.simpleRegistView.securityCode.text;
    // 注册用户
    BmobUser *bUser = [[BmobUser alloc]init];
    bUser.username = phoneNumber;
    bUser.password = userPassword;
    [bUser signUpInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful)
        {
            NSLog(@"注册成功");
        }
        else
        {
            NSLog(@"注册失败");
        }
    }];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
// 验证失败
- (void)verityFail
{
    
}
@end
